package com.zang.tree;

/**
 * 修剪二叉搜索树
 * @author ZVerify
 * @since 2022/11/03 09:12
 * @see <a href="https://leetcode.cn/problems/trim-a-binary-search-tree/">...</a>
 **/
public class 修剪二叉搜索树 {
    public TreeNode trimBST(TreeNode root, int low, int high) {

        return bfs(root,low,high);
    }

    private TreeNode bfs(TreeNode root, int low, int high) {

        if (root == null) return null;

        if (root.val<low) {

            return bfs(root.left,low,high);
        }
        if (root.val>high) {
            return bfs(root.right,low,high);
        }
        root.left = bfs(root.left,low,high);
        root.right = bfs(root.right,low,high);
        return root;
    }
}
